package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.IProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品类别 前端控制器
 * </p>
 *
 * @author biu
 * @since 2025-03-13
 */

@Tag(name = "商品类别")
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private IProductUnitService productUnitService; ;

    /**
     * 分类品牌列表
     */
    @Log(title = "商品类别", businessType = BusinessType.QUERY)
   // @RequiresPermissions("product:productUnit:list")
    //   @RequiresRoles("admin")    //角色
    @Operation(summary = "商品列表")
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(name = "name" ,description = "商品单位名称") String name)
    {
        //分页
        startPage();

        //查询
        List<ProductUnit> list = productUnitService.selectproductUnitList(name);
        return getDataTable(list);
    }

    //    @RequiresRoles("productAdmin")
//    @RequiresLogin
  //  @RequiresPermissions("product:productUnit:query")
    @Operation(summary = "id获取商品单位详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@Parameter(description = "商品id") @PathVariable("id") Long id){

        //为前端封装一个正确的结果，并封装需要返回的数据
        return success(productUnitService.getById(id)); //返回有结果的数据
    }

    @Log(title = "商品类别", businessType = BusinessType.DELETE)
    @RequiresPermissions("product:productUnit:remove")
    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @Parameter(description = "商品id列表")
            @PathVariable List<Long> ids) {
        return toAjax(productUnitService.removeBatchByIds(ids));
    }

    /**
     * 新增品牌
     */
   // @RequiresPermissions("product:productUnit:add")
    @Log(title = "商品类别", businessType = BusinessType.INSERT)
    @Operation(summary = "新增商品")
    @PostMapping
    public AjaxResult add(/*@Validated */@RequestBody ProductUnit productUnit)
    {

        productUnit.setCreateBy(SecurityUtils.getUsername());

        //如果业务成功则为前端返回一个响应码是200，且数据结果是null的结果对象
        //如果业务失败则为前端返回一个响应码是500，且数据结果是null的结果对象
        return toAjax(productUnitService.save(productUnit));
    }

    /**
     * 修改品牌
     */
  //  @RequiresPermissions("product:productUnit:edit")
    @Log(title = "商品类别", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult edit(/*@Validated*/ @RequestBody ProductUnit productUnit)
    {

        productUnit.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.updateById(productUnit));
    }

    @Operation(summary = "获取全部单位")
    @GetMapping("/getUnitAll")
    public AjaxResult selectProductUnitAll() {
        return success(productUnitService.list());
    }
}
